import { Injectable } from '@angular/core';
import { APIBase } from 'app/modules/base/api-base';
import { HttpClient } from '@angular/common/http';
import {
  TaskType,
  TaskPeriod,
  TaskTarget,
  TaskService,
} from 'app/modules/task/services/task.service';

@Injectable({
  providedIn: 'root',
})
export class ComputerService extends APIBase<Computer, ComputerInfo> {
  constructor(protected http: HttpClient, private taskService: TaskService) {
    super(http, '/api/admin/pc');
  }
  installed(cid: number) {
    return new APIBase(this.http, '/api/admin/installed/software');
  }
  upgrade(opt: { zoneIDs?: number[]; computerIDs?: number[] }) {
    const conf = {
      type: TaskType.SystemUpgrade,
      target: TaskTarget.Computer,
      period_type: TaskPeriod.Now,
      computers: [],
      areas: [],
    };
    if (opt.computerIDs) {
      conf.computers = opt.computerIDs.map(id => ({ id }));
    }
    if (opt.zoneIDs) {
      conf.target = TaskTarget.AreaComputer;
      conf.areas = opt.zoneIDs.map(id => ({ id }));
    }
    return this.taskService.create(conf);
  }

  pc_use(cid: number) {
    return new APIBase(this.http, this.baseURL + `/${cid}/pc_use`);
  }
  //终端可登陆人员列表
  terminal_per(cid: number) {
    return new APIBase(this.http, this.baseURL + `/${cid}/user`);
  }
  //人员
  allPerson(url){
    return new APIBase(this.http,url)
  }
  //选择添加人员
  createPerson(url,ids) {
    return this.http.post(url,ids).toPromise();
  }
}

export interface Computer {
  id: number;
  name: string;
  remark: string;
  device_type:number;
}


export enum ComputerStatus {
  Enable,
  Disable,
}

export interface ComputerInfo {
  id: number;
  name: string;
  remark: string;
  status: number;
  machine_id: string;
  os: string;
  mainboard: Mainboard;
  cpu_list: Mainboard[];
  memory_list: Memorylist[];
  disk_list: Memorylist[];
  graphics_list: any[];
  network_device_list: Networkdevicelist[];
  created_at: string;
  updated_at: string;
  created_at_time: string;
  updated_at_time: string;
}

interface Networkdevicelist {
  mac: string;
  pc_id: number;
  model: string;
  ip: string;
}

interface Memorylist {
  sn: string;
  pc_id: number;
  model: string;
  cap: number;
}

export interface Zone {
  id?: number;
  level_struct?: string;
  name?: string;
  des?: string;
  user_count?: number;
  pc_count?: number;
  software_count?: number;
  children?: Zone[];
  area_id?: number;
  checked?: boolean;
  fname?: string
  grade?: number
  had_child?: number
  loading?: false
  parent_id?: number
  trees?: string
}
export interface ZoneDetail extends Zone {
  id: number;
  name: string;
  parent_area_name: string;
  des: string;
  create_admin_username: string;
  created_at: string;
  user_count: number;
  new_user_count: number;
  pc_count: number;
  new_pc_count: number;
  software_count: number;
  new_software_count: number;
  auth_status: number;
}
export interface Personnel {
  area_count: number
  full_name:string
  id:number
  job_number: string
  online: boolean
  remark: string
  status: number
  update_admin_username: string
  updated_at: string
  utype: number
}
interface Mainboard {
  sn: string;
  pc_id: number;
  model: string;
}
